package com.eshop.modules.product.service.impl;

import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.product.domain.StoreProduct;
import com.github.pagehelper.PageInfo;
import com.eshop.modules.product.service.dto.StoreProductRelationDto;
import com.eshop.domain.PageResult;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.product.service.dto.StoreProductRelationQueryCriteria;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import java.io.Serializable;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.eshop.api.EshopException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.modules.product.vo.StoreProductRelationQueryVo;
import java.util.List;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.user.service.UserService;
import com.eshop.modules.product.service.StoreProductService;
import org.slf4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.product.service.StoreProductRelationService;
import com.eshop.modules.product.domain.StoreProductRelation;
import com.eshop.modules.product.service.mapper.StoreProductRelationMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(rollbackFor = { Exception.class })
public class StoreProductRelationServiceImpl extends BaseServiceImpl<StoreProductRelationMapper, StoreProductRelation> implements StoreProductRelationService
{
    private static final Logger log;
    private final StoreProductRelationMapper storeProductRelationMapper;
    private final StoreProductService storeProductService;
    private final UserService userService;
    private final IGenerator generator;
    
    public List<StoreProductRelationQueryVo> userCollectProduct(final int page, final int limit, final Long uid, final String type) {
        final Page<StoreProductRelation> pageModel = (Page<StoreProductRelation>)new Page((long)page, (long)limit);
        final List<StoreProductRelationQueryVo> list = this.storeProductRelationMapper.selectRelationList(pageModel, uid, type);
        return list;
    }
    
    public void addRroductRelation(final long productId, final long uid, final String category) {
        if (this.isProductRelation(productId, uid)) {
            throw new EshopException("已收藏");
        }
        final StoreProductRelation storeProductRelation = StoreProductRelation.builder().productId(productId).uid(uid).type(category).build();
        this.storeProductRelationMapper.insert(storeProductRelation);
    }
    
    public void delRroductRelation(final long productId, final long uid, final String category) {
        final StoreProductRelation productRelation = (((this.lambdaQuery().eq(StoreProductRelation::getProductId, productId)).eq(StoreProductRelation::getUid, uid)).eq(StoreProductRelation::getType, category)).one();
        if (productRelation == null) {
            throw new EshopException("已取消");
        }
        this.removeById(productRelation.getId());
    }
    
    public Boolean isProductRelation(final long productId, final long uid) {
        final int count = this.storeProductRelationMapper.selectCount(((Wrappers.<StoreProductRelation>lambdaQuery().eq(StoreProductRelation::getUid, uid)).eq(StoreProductRelation::getType, "collect")).eq(StoreProductRelation::getProductId, productId));
        if (count > 0) {
            return true;
        }
        return false;
    }
    
    public PageResult<StoreProductRelationDto> queryAll(final StoreProductRelationQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreProductRelation> page = (PageInfo<StoreProductRelation>)new PageInfo((List)this.queryAll(criteria));
        final PageResult<StoreProductRelationDto> relationDtoPageResult = (PageResult<StoreProductRelationDto>)this.generator.convertPageInfo((PageInfo)page, (Class)StoreProductRelationDto.class);
        relationDtoPageResult.getContent().forEach(i -> {
            i.setProduct((StoreProduct)this.storeProductService.getById((Serializable)i.getProductId()));
            i.setUserName(this.userService.getYxUserById(i.getUid()).getNickname());
            return;
        });
        return relationDtoPageResult;
    }
    
    public List<StoreProductRelation> queryAll(final StoreProductRelationQueryCriteria criteria) {
        return (List<StoreProductRelation>)((StoreProductRelationMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreProductRelation.class, criteria));
    }
    
    public void download(final List<StoreProductRelationDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreProductRelationDto yxStoreProductRelation : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("用户ID", yxStoreProductRelation.getUid());
            map.put("商品ID", yxStoreProductRelation.getProductId());
            map.put("类型(收藏(collect）、点赞(like))", yxStoreProductRelation.getType());
            map.put("某种类型的商品(普通商品、秒杀商品)", yxStoreProductRelation.getCategory());
            map.put("添加时间", yxStoreProductRelation.getCreateTime());
            map.put(" updateTime", yxStoreProductRelation.getUpdateTime());
            map.put(" isDel", yxStoreProductRelation.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreProductRelationServiceImpl(final StoreProductRelationMapper storeProductRelationMapper, final StoreProductService storeProductService, final UserService userService, final IGenerator generator) {
        this.storeProductRelationMapper = storeProductRelationMapper;
        this.storeProductService = storeProductService;
        this.userService = userService;
        this.generator = generator;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)StoreProductRelationServiceImpl.class);
    }
}
